package pais.www.stat;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import pais.db.*;
import papos.rowset.KeyNameNotFoundException;
import papos.rowset.StorageSystem;
import pasys.error.PaposException;

class PrintKeyStat {
  public static void printKeyStat(PrintWriter toClient, StringTokenizer stok) throws PaposException  {
    toClient.println("printing keyword stat.");
    long totalSize = 0;
    long sameKeyCount = 0;
    long count = 0;
    KeywordTable keywordTable = KeywordTable.getInstance();
    Keyword kw = new Keyword();
    StorageSystem storage = keywordTable.getStorage();
    try {
      if (storage.findFirst("keyword_", kw)) {
        String beforeKey = "";
        String currentKey = "";
        do {
          currentKey = kw.getKeyword();

          totalSize += currentKey.length();

          if (currentKey.equals(beforeKey)) {
            sameKeyCount ++;
          }

          beforeKey = currentKey;
          count++;
        } while (storage.findNext("keyword_", kw));

        toClient.println( " average key size = "+ (double)totalSize/(double)count );
        toClient.println( " total key count = "+ count );
        toClient.println( " same key count = "+ sameKeyCount );
        toClient.println( " same key percent = "+ sameKeyCount * 100 / count +" %");
      }

    } catch (KeyNameNotFoundException e) {
      assert(false);
    }

  }

}
